<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;
use App\Collect;

class User extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        if (method_exists($this, $request->method) && $method = $request->method) {
            return $this->$method();
        } else {
            return $this->getInfo();
        }
    }


    public function getInfo()
    {
        return [
            'user_id' => $this->id,
            'name' => $this->name,
            'avatar' => getUserAvatar($this->avatar),
            'sign' => $this->sign,
            'datanum' => $this->datanum,
        ];
    }

    public function getuser()
    {
        return [
            'user_id' => $this->id,
            'name' => $this->name,
            'coins' => $this->coins,
            'is_vip' => $this->is_vip,
            'money' => $this->money,
            'tixianmoney' => $this->tixianmoney,
            'vipduetime' => $this->vipduetime->toDateString(),
            'avatar' => getUserAvatar($this->avatar),
            'mobile' => $this->mobile,
            'tixianaccount' => $this->alipay_account,
            'earnings' => $this->money + $this->tixianmoney,
            'realname' => $this->realname,
            'sign' => $this->sign,
            'app_url' => env('APP_URL'),
            'qqname' => $this->qqname,
            'wechatname' => $this->wechatname,
            'datacollects' => Collect::where('user_id', $this->id)->where('commentable_type', 'App\\Data')->pluck('commentable_id'),
            'notifications' => $this->unreadNotifications()->count(),
            'articlecollects' => Collect::where('user_id', $this->id)->where('commentable_type', 'App\\Article')->pluck('commentable_id'),
        ];
    }


}
